/*
 * @Author: Joker 
 * @Date: 2017-11-28 12:19:13 
 * @Last Modified by: Joker
 * @Last Modified time: 2018-01-04 16:54:27
 */
import {
  login
} from "../../service/service_zbw";
export default {
  /**
   * 获取人员拥有的部门
   */
  async GetUserDept({
    commit,
    state
  }) {
    let res = await login.getDeptByUserId();
    if (res.success) {
      try {
        for (let i of res.obj) {
          i.id = i.deptId;
          i.deptDesc = i.deptDesc || '';
        }
        commit('SET_USERDEPT', res.obj);
      } catch (error) {
        console.log(error)
        commit('SET_USERDEPT', null);
      }
    } else {
      commit('SET_USERDEPT', null);
      this.$message({
        type: 'error',
        message: res.msg
      });
    }
    return res;
  },
  async GetUserRealDept({
    commit,
    state
  }) {
    let res = await login.getRealDeptByUserId();
    if (res.success) {
      try {
        for (let i of res.obj) {
          i.id = i.deptId;
          i.deptDesc = i.deptDesc || '';
        }
        commit('SET_USERREALDEPT', res.obj);
      } catch (error) {
        console.log(error)
        commit('SET_USERREALDEPT', null);
      }
    } else {
      commit('SET_USERREALDEPT', null);
      this.$message({
        type: 'error',
        message: res.msg
      });
    }
    return res;
  },
  /**
   * 角色列表
   */
  async GetRoleList({
    commit,
    state
  }) {
    let res = await login.getRolesList();
    if (res.success) {
      try {
        for (let i of res.obj) {
          i.id = i.roleId;
        }
        commit('SET_ROLELIST', res.obj);
      } catch (error) {
        console.log(error)
        commit('SET_ROLELIST', null);
      }
    } else {
      commit('SET_ROLELIST', null);
      this.$message({
        type: 'error',
        message: res.msg
      });
    }
    return res;
  },
  async GetRoleIdList({
    commit,
    state
  }) {
    let res = await login.getRoleList();
    if (res.success) {
      console.log('权限数组____________________', res)
      try {
        commit('SET_ROLEIDLIST', res.obj);
      } catch (error) {
        console.log(error)
        commit('SET_ROLEIDLIST', null);
      }
    } else {
      commit('SET_ROLEIDLIST', null);
      this.$message({
        type: 'error',
        message: res.msg
      });
    }
    return res;
  },
  /**
   * 角色树
   */
  //   async GetRoleTree({
  //     commit,
  //     state
  //   }) {
  //     let res = await login.getRolesTree();
  //     if (res.success) {
  //       try {
  //         commit('SET_ROLETREE', res.obj);
  //       } catch (error) {
  //         console.log(error)
  //         commit('SET_ROLETREE', null);
  //       }
  //     } else {
  //       commit('SET_ROLETREE', null);
  //     }
  //     return res;
  //   },
  /**
   * 角色可选权限列表
   */
  async GetRolePowerList({
    commit,
    state
  }) {
    let res = await login.getMenuList();
    if (res.success) {
      try {
        commit('SET_ROLEPOWERALL', res.obj);
      } catch (error) {
        console.log(error)
        commit('SET_ROLEPOWERALL', null);
      }
    } else {
      commit('SET_ROLEPOWERALL', null);
      this.$message({
        type: 'error',
        message: res.msg
      });
    }
    return res;
  },
  /**
   * 角色可选权限列表
   */
  async GetUserPowers({
    commit,
    state
  }) {
    let res = await login.getUserPowers();
    if (res.success) {
      console.log('用户的权限啊', res)
      try {
        commit('SET_USERROLE', res.obj);
      } catch (error) {
        console.log(error)
        commit('SET_USERROLE', null);
      }
    } else {
      commit('SET_USERROLE', null);
      this.$message({
        type: 'error',
        message: res.msg
      });
    }
    return res;
  },
  /**
   * 清除所有缓存
   */
  clearAll({
    commit,
    state
  }) {
    commit('SET_USERDEPT', null);
    commit('SET_ROLELIST', null);
    commit('SET_ROLEIDLIST', null);
    commit('SET_ROLEPOWERALL', null);
    commit('SET_USERROLE', null);
    commit('SET_PROJECTID', null);
  }
}

